package jnet;

import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import javax.swing.ImageIcon;

/**
 *
 * @author Supratim Bandyopadhyaya http://www.linkedin.com/in/linksupratim
 */
public class JNet {

    protected static Boolean isExit = Boolean.FALSE;
    protected static TrayIcon trayIcon;
    static Image okImage = getImage("/available.png");
    static Image problemImage = getImage("/unavailable.png");
    static String okMessage = "You are connected to the Internet";
    static String problemMessage = "OOPS! It seems like you are not connected to the internet";
    static String dialogMessage = "jNet";
    static Boolean isOnline = Boolean.FALSE;

    public static void main(String[] args) {

        if (SystemTray.isSupported()) {

            trayIcon = prepareUI();


        } else {
            throw new RuntimeException("System tray is not supported on this platform!!");

        }

    }

    public static Image getImage(String imagename) {
        ImageIcon icon = new ImageIcon(JNet.class.getResource(imagename));
        return icon.getImage();
    }

    public static boolean testInet(String site) {
        Socket sock = new Socket();
        InetSocketAddress addr = new InetSocketAddress(site, 80);
        try {
            sock.connect(addr, 3000);
            return true;
        } catch (IOException e) {
            return false;
        } finally {
            try {
                sock.close();
            } catch (IOException e) {
            }
        }
    }

    private static TrayIcon prepareUI() {
        //////////////prepare ui//////////////



        final SystemTray systemTray = SystemTray.getSystemTray();
        final TrayIcon trayIcon = new TrayIcon(problemImage,
                dialogMessage);
        trayIcon.setImageAutoSize(true);


        PopupMenu menu = new PopupMenu();
        MenuItem exit = new MenuItem("Exit");
        exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                isExit = Boolean.TRUE;
                System.exit(0);

            }
        });
        menu.add(exit);
        trayIcon.setPopupMenu(menu);





        MouseAdapter mouseAdapter = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                // This will display small popup message from System Tray
                trayIcon.displayMessage("jNet",
                        dialogMessage,
                        TrayIcon.MessageType.INFO);
            }
        };

        trayIcon.addMouseListener(mouseAdapter);

        try {
            systemTray.add(trayIcon);

            ConnectionCheckWorker cw = new ConnectionCheckWorker();
            cw.execute();



        } catch (Exception e) {
            e.printStackTrace();
        }

        /////////////////////////////////////
        return trayIcon;
    }
}
